<template>
	<view class="wrap">
		<u-navbar
			title="联系信息填写"
			@rightClick="rightClick"
			:autoBack="true"
		>
		</u-navbar>
		<u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType">
			<u-form-item label-width="160" :label-position="labelPosition" label="姓名" prop="name">
				<u-input :border="border" placeholder="请输入联系人姓名" v-model="model.name" type="text"></u-input>
			</u-form-item>
			<u-form-item :label-position="labelPosition" label="手机号码" prop="phone" label-width="160">
				<u-input :border="border" placeholder="请输入手机号" v-model="model.phone" type="number"></u-input>
			</u-form-item>
			<u-form-item :label-position="labelPosition" label="详细地址" prop="address" label-width="160">
				<u-input type="textarea" :border="border" placeholder="请输入详细地址" v-model="model.intro" />
			</u-form-item>
			<u-form-item :label-position="labelPosition" label="备注" prop="intro" label-width="160">
				<u-input type="textarea" :border="border" placeholder="请输入备注" v-model="model.intro" />
			</u-form-item>
		</u-form>
		<u-button @click="submit">提交</u-button>
		<u-action-sheet :list="actionSheetList" v-model="actionSheetShow" @click="actionSheetCallback"></u-action-sheet>
		<u-select mode="single-column" :list="selectList" v-model="selectShow" @confirm="selectConfirm"></u-select>
		<u-picker mode="region" v-model="pickerShow" @confirm="regionConfirm"></u-picker>
	</view>
</template>

<script>
	export default {
		data() {
			let that = this;
			return {
				rules: {
					name: [
						{
							required: true,
							message: '请输入姓名',
							trigger: 'blur' ,
						},
						{
							min: 3,
							max: 5,
							message: '姓名长度在3到5个字符',
							trigger: ['change','blur'],
						},
						{
							// 此为同步验证，可以直接返回true或者false，如果是异步验证，稍微不同，见下方说明
							validator: (rule, value, callback) => {
								// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
								return this.$u.test.chinese(value);
							},
							message: '姓名必须为中文',
							// 触发器可以同时用blur和change，二者之间用英文逗号隔开
							trigger: ['change','blur'],
						},
					],
					intro: [
						{
							required: true,
							message: '请填写备注'
						},
						{
							min: 0,
							message: '备注不能少于5个字',
							trigger: 'change' ,
						},
						// 正则校验示例，此处用正则校验是否中文，此处仅为示例，因为uView有this.$u.test.chinese可以判断是否中文
						{
							pattern: /^[\u4e00-\u9fa5]+$/gi,
							message: '简介只能为中文',
							trigger: 'change',
						},
					],
					address: [
						{
							required: true,
							message: '请填写详细地址'
						},
						{
							min: 10,
							message: '备注不能少于5个字',
							trigger: 'change' ,
						},
						// 正则校验示例，此处用正则校验是否中文，此处仅为示例，因为uView有this.$u.test.chinese可以判断是否中文
						{
							pattern: /^[\u4e00-\u9fa5]+$/gi,
							message: '简介只能为中文',
							trigger: 'change',
						},
					],
					phone: [
						{
							required: true,
							message: '请输入手机号',
							trigger: ['change','blur'],
						},
						{
							validator: (rule, value, callback) => {
								// 调用uView自带的js验证规则，详见：https://www.uviewui.com/js/test.html
								return this.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change，二者之间用英文逗号隔开
							trigger: ['change','blur'],
						}
					],
				border: false,
				selectStatus: 'close',
				actionSheetShow: false,
				pickerShow: false,
				selectShow: false,
				radioCheckWidth: 'auto',
				radioCheckWrap: false,
				labelPosition: 'left',
				codeTips: '',
				errorType: ['message'],
				}
			};
		},
		computed: {
			borderCurrent() {
				return this.border ? 0 : 1;
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			submit: function() {
				// uni.request({
				// 	url: _self.server_host + '/api',
				// 	method: 'POST',
				// 	header: {'content-type' : "application/x-www-form-urlencoded"},
				// 	data: {
				// 		// 传参方式一：以JSON字符串的形式提交form表单数据
				// 		formData: JSON.stringify(_self.formData),
				// 		// 传参方式二：将form表单数据以对象形式传递
				// 		// formData: _self.formData,
				// 	},
				// 	success: res => {
				// 		// 服务器返回数据，后续业务逻辑处理
				// 	},
				// 	fail: () => {
				// 		uni.showToast({ title: "服务器响应失败，请稍后再试！", icon : "none"});
				// 	},
				// 	complete: () => {}
				// });
				console.log('提交成功')
			},
			rightClick(){
				uni.navigateBack({
					delta:1
				})
			},
			// 点击actionSheet回调
			actionSheetCallback(index) {
				uni.hideKeyboard();
				this.model.sex = this.actionSheetList[index].text;
			},
			// 选择地区回调
			regionConfirm(e) {
				this.model.region = e.province.label + '-' + e.city.label + '-' + e.area.label;
			},
			codeChange(text) {
				this.codeTips = text;
			}
		}
	};
</script>

<style>
	page{
		background-color: #fff;
	}
	.wrap {
		padding: 30rpx;
	}
	
	.agreement {
		display: flex;
		align-items: center;
		margin: 40rpx 0;
	}
	.agreement-text {
			padding-left: 8rpx;
			color: $u-tips-color;
		
	}
	
</style>